// pages/order/orderinfo/orderinfo.js
const App = getApp()
let { AJAX, timeTranfer, imgUrl } = require('../../../utils/util.js')

Page({
   
  /**
   * 页面的初始数据
   */
  data: {
    order_sn:'', 
    navH: '',
    dataInfo: {},
    imgUrl: imgUrl
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      order_sn:options.sn,
      navH: App.globalData.navHeight

    })
    this.requestInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  navBack(){
    wx.switchTab({
       url: '/pages/mine/mine',
    })
  },
  topay(){
    // pay/enter/编号-支付类型-类型 ?code=值
    let vm = this
    // 登录
    wx.login({
      success: e => {
        let _path = 'pay/enter/' + vm.data.order_sn + '-order?code=' + e.code
        AJAX(_path, 'get', {
        }).then(res => {
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            success(res) {
              vm.requestInfo()
             },
            fail(res) { }
          })
        })
      }
    })
  },
  editStudent(e){
    // 编辑学生信息
    let vm = this
    let _index = e.currentTarget.dataset.index
    wx.setStorageSync("CUSTOMERS", JSON.stringify(vm.data.dataInfo.customers))
    wx.navigateTo({
      url: '/pages/order/editstudent/editstudent?index=' + _index + "&id=" + vm.data.dataInfo.order_id,
    })
  },
  requestInfo(){
    let vm =this
    let AJAX = require('../../../utils/util.js').AJAX
    AJAX('order/info', 'get', {
      order_sn: vm.data.order_sn
    }).then(res=>{
      let _data = res.data
       console.log(_data)
       if (_data.play_time != null){
         _data.play_time = timeTranfer(_data.play_time, 1)
      }
      _data.product.begin_time = timeTranfer(_data.product.begin_time)
      _data.product.end_time = timeTranfer(_data.product.end_time)
      _data.create_time = timeTranfer(_data.create_time)
      this.setData({
        dataInfo: _data
      })
    })
  },
  toCancle(e) {
    let vm = this
    let _sn = e.currentTarget.dataset.id
    AJAX("order/cancel", "POST", {
      order_sn: _sn
    }).then(res => {
      vm.requestInfo()
    })
  },
})